#!/bin/bash
#
# Script Interface.
# Starts all Scripts which are necessary for the running webserver.
#
#
#
#
#cur_path=$(pwd)
cur_path="/var/www/scripts"
user="www-data"



if [ "$1" = "start" ]
then
	echo start the Terra control system...
	
	chown -R $user:$user /var/www 
	chown root:$user /usr/bin/mjpg-streamer
	chmod u+s /usr/bin/mjpg-streamer
	
	
	# Start automatical WAN IP sending via e-Mail #
	cd $cur_path/getwan 
	./getwan & 

	# Ping router and syncronise time #
	cd $cur_path/ping
	./ping & 

	# Start Stream # 
	#mjpg-streamer -i "/lib/input_uvc.so -d /dev/video0  -q 65 -f 15  -r 320x240" -o "/lib/output_http.so -p 8080 -w /var/www/stream -n" -b

	# Start Terra System Controll daemon(controll steps and datalogging) #
	cd $cur_path/terra_control
	chmod -R 0777 /var/spool/cron
	./control-systemd & 
	

fi

if [ "$1" = "stop" ]
then
	    echo stop all web daemons
        /bin/kill "$(pidof -x ./getwan)"
		echo "getwan stopped"

        /bin/kill "$(pidof -x ./ping)"
        echo "ping stopped"

        /bin/kill "$(pidof mjpg-streamer)"
        echo "mjpg-streamer stopped"

        /bin/kill "$(pidof -x ./control-systemd)"
        echo "control-systemd stopped"


        /usr/bin/killall sleep
        echo "all sleep processes stopped"	
fi

 
if [ "$1" = "stream-start" ]
then
	echo restart not implementet, yet.
	/bin/kill "$(pidof mjpg-streamer)"
	mjpg-streamer -i "/lib/input_uvc.so -d /dev/video0  -f 15  -r 600x800" -o "/lib/output_http.so -p 8080 -w /var/www/stream -n" -b
fi

if [ "$1" = "stream-stop" ]
then
	/bin/kill "$(pidof mjpg-streamer)"
fi
